﻿requirejs.config({
    baseUrl: ".",
    paths: {
    	zepto:"javascripts/zepto.min",
    	dingtalk:"javascripts/dingtalk",
        vue: "components/vue/vue.min",
        axios: "components/axios/axios.min",
        MINT: "components/mint-ui/index",
        common: "resources/js/common"
    }
});

require(["zepto","dingtalk","vue", "axios", "common", "MINT"], function (zepto,dingtalk,Vue, axios, common, MINT) {

    Vue.component(MINT.Popup.name, MINT.Popup);
    Vue.component(MINT.Toast.name, MINT.Toast);

    var vm = new Vue({
        el: "#view",
        data: {
            detail: {
            },
            commentaryVisible: false,
            commentaryType: 0,
            comment: "",
            showComment: true,
            comments: [],
            avgRate: 5,
            isHelp: false
        },
        mounted: function () {
            var self = this;

            var id = common.getParam("id");

            axios.get("knowledge/detail?id=" + id)
                .then(function (response) {
                    self.detail = response.data.data;
                    self.isHelp = self.detail.treenode_name_path.indexOf("问题求助") != -1;
                    self.comments = response.data.comments;
                    if (self.comments.length > 0) {
                        var totalRate = 0;
                        for (var i = 0; i < self.comments.length; i++) {
                            totalRate += self.comments[i].comment_type;
                        }
                        self.avgRate = (totalRate / self.comments.length).toFixed(1);
                    }
                });
        },
        methods: {
            commentary: function (type) {
                this.commentaryType = type;
                this.commentaryVisible = true;
            },
            popupCommentary: function () {
                var self = this;

                var data = {
                    id: this.detail.id,
                    commentaryType: this.commentaryType,
                    comment: this.comment
                }
                if (common.isEmpty(data.comment)) {
                    MINT.Toast("请输入理由");
                    return;
                }

                axios.post("knowledge/comment", data)
                    .then(function (response) {
                        self.detail[self.commentaryType] += 1;
                        self.commentaryVisible = false;
                        self.showComment = false;
                        MINT.Toast("评论已提交");
                    });
            }
        }
    });
});